//
//  ZQSQLiteTools.swift
//  ZQWeibo
//
//  Created by summer的Dad on 16/11/26.
//  Copyright © 2016年 summer的Dad. All rights reserved.
//

import UIKit

class ZQSQLiteTools: NSObject {
    //单例
    static let shared : ZQSQLiteTools = ZQSQLiteTools()
    //创建数据库文件并且打开数据库连接
    let queue : FMDatabaseQueue
    //单利对象一但创建就打开数据
    override init() {
       
        
        let path = (NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask, true).last! as NSString).appendingPathComponent("demo.db")

        queue = FMDatabaseQueue(path: path)
        
        
        super.init()
        
        createTable()
    }
    
    
    
    private func createTable(){
        
          let sql = "CREATE TABLE IF NOT EXISTS T_Status (statusId INTEGER PRIMARY KEY NOT NULL, status TEXT, userId TEXT, create_date TEXT DEFAULT (datetime('now','localtime')));"
        
        queue.inTransaction { (db, rollback) in
            //rollbacks会回滚的意思
            let res = db!.executeStatements(sql)
            
            if res {
                
                print("数据库创建成功")
            }else{
                
                
                print("创建数据库失败")
                //回滚指针指向的值
                rollback?.pointee = true
            }
            
        }
        
        
        
    }

}
